package com.david.zhufengfm;


import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.david.zhufengfm.Fragments.CustomTingFragment;
import com.david.zhufengfm.Fragments.DiscoverFragment;
import com.david.zhufengfm.Fragments.DownloadTingFragment;
import com.david.zhufengfm.Fragments.PersonalFragment;

import static com.david.zhufengfm.R.id.main_tab_bar;

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {

    /**
     * 主界面中第一层的Fragment,发现,定制听,下载听,我
     */
    private Fragment[] fragments;
    private RadioButton radioButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //TODO 检查横竖屏切换时，这个数组对象和内部元素是否置空

        fragments=new Fragment[4];
        FragmentManager manager = getSupportFragmentManager();
        if (savedInstanceState != null) {
            //代表Activity重新创建，内部的F会自动添加
            int len=fragments.length;
            for (int i = 0; i < len; i++) {
                fragments[i]=manager.findFragmentByTag("f"+i);
                manager.beginTransaction().hide(fragments[i]);
            }
        }else {
            fragments[0] = new DiscoverFragment();
            fragments[1] = new CustomTingFragment();
            fragments[2] = new DownloadTingFragment();
            fragments[3] = new PersonalFragment();

            FragmentTransaction tx = manager.beginTransaction();
            int length = fragments.length;
            for (int i = 0; i < length; i++) {
                //添加Fragment，并且指定Tag，利用Tag。在Activity重新创建的时候
                //进行恢复查找
                tx.add(R.id.main_fragment_container, fragments[i],"f"+i);
                tx.hide(fragments[i]);
            }
            tx.show(fragments[0]);
            tx.commit();
        }
        RadioGroup radioGroup= (RadioGroup) findViewById(R.id.main_tab_bar);
        radioButton = (RadioButton) radioGroup.getChildAt(0);
        radioButton.setChecked(true);
//        showFragment("discover",DiscoverFragment.class);

        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int index=0;
        switch (checkedId){
            case R.id.main_tab_item_discover:
                radioButton.setChecked(true);
                index=0;
//                showFragment("discover",DiscoverFragment.class);
                break;
            case R.id.main_tab_item_custom:
                radioButton.setChecked(false);
                index=1;
//                showFragment("customting",CustomTingFragment.class);
                break;
            case R.id.main_tab_item_download:
                radioButton.setChecked(false);
                index=2;
//                showFragment("downloadting",DownloadTingFragment.class);
                break;
            case R.id.main_tab_item_personal:
                radioButton.setChecked(false);
                index=3;
//                showFragment("personal",PersonalFragment.class);
                break;

        }
        int length=fragments.length;
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        for (int i = 0; i < length; i++) {
            if(i==index){
                transaction.show(fragments[i]);
            }else{
                transaction.hide(fragments[i]);
            }
        }
        transaction.commit();
    }
//    private Fragment curFragment=null;//当前正在查看的fragment
//    private void showFragment(String tag,Class<? extends Fragment> modelFragment) {
//        Fragment  f=fm.findFragmentByTag(tag);
//        if(f==null){
//            //首次显示
//            try {
//                f=modelFragment.newInstance();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//            if(curFragment==null){
//                //显示的第一页面
//                fm.beginTransaction()
//                        .add(R.id.main_fragment_container, f, tag)
//                        .commit();
//            }else{
//                //隐藏当前正显示的Fragment,并增加第一次显示的model1-f
//                fm.beginTransaction()
//                        .hide(curFragment)
//                        .add(R.id.main_fragment_container, f,tag)
//                        .commit();
//            }
//        }else{ //之前已显示过了，再次显示
//            if(curFragment==f) return;
//
//            fm.beginTransaction()
//                    .hide(curFragment)
//                    .show(f)
//                    .commit();
//        }
//
//        curFragment=f;
//    }


}
